﻿using Models.Mailbox;
using Models.Mailbox.Mail;
using Newtonsoft.Json;
using SendMessagePage.Util;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SendMessagePage.HomePage
{
    /// <summary>
    /// SendMail.xaml 的交互逻辑
    /// </summary>
    public partial class SendMail : Window
    {
        //查询邮箱配置
        private string TimeTaskEmailUrl = ConfigurationManager.AppSettings["TimeTaskEmail"];
        public SendMail()
        {
            InitializeComponent();
            Closing += Window_Closing;
        }



        private async void SendMail_Click(object sender, RoutedEventArgs e)
        {
            string title = this.txtTitle.Text.Trim();
            string content = this.txtContent.Text.Trim();
            string addressee = this.txtAddressee.Text;

            //ckChecked = ckPwd.IsChecked.Value;

            if (title.Length == 0)
            {
                MessageBox.Show("标题内容不能为空", "登录提示");
                this.txtTitle.Focus();
                return;
            }

            if (content.Length == 0)
            {
                MessageBox.Show("正文内容不能为空", "登录提示");
                this.txtContent.Focus();
                return;
            }

            if (addressee.Length == 0)
            {
                MessageBox.Show("收件人内容不能为空", "登录提示");
                this.txtAddressee.Focus();
                return;
            }
            
            if (MainWindow.order==null || MainWindow.order.result == null) {
                MessageBox.Show("请完善个人邮箱配置！");
            }
            //有个bug,必须先进入配置中，不然会报错

            MailMessage mail = new MailMessage() {
                FromEmail = MainWindow.order.result[0].FromEmail,
                PassWord = MainWindow.order.result[0].ExcelPassWord,
                ToEmail = addressee,
                Subject = this.txtTitle.Text,
                Body = this.txtContent.Text,
                Host = MainWindow.order.result[0].SmtpServe,
                Ip = MainWindow.order.result[0].SmtpPort,
                EnableSsl = true,
                UseDefaultCredentials = false,

            };
            string json =   JsonConvert.SerializeObject(mail);

            string result = await AccessWebApiUtil.PostAsyncJson(TimeTaskEmailUrl, json, PackingUserLogin.token);

            OrderResult order =  JsonConvert.DeserializeObject<OrderResult>(result);

            if (order.result ==null || !order.code.Equals(200)) {
                MessageBox.Show("发送失败！");
            }
            MessageBox.Show("发送成功!");
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            MainWindow mainWindow = new MainWindow();
            if (MessageBox.Show("是否要关闭？", "确认", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                e.Cancel = false;
                mainWindow.Show();


            }
            else
            {
                e.Cancel = true;
            }



        }
    }
}
